/*
 * Hud.java
 *
 * Created on 16. Dezember 2006, 19:07
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.graphics.ortho.hud;

import com.jme.math.FastMath;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.shape.Quad;
import com.jme.system.DisplaySystem;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import kanjitori.LevelManager;
import kanjitori.util.Cache;

/**
 *
 * @author Pirx
 */
public class DefaultHud extends AbstractHud {
    
    private final static Image TELEPORT_IMAGE = new ImageIcon("data/hud_tele.png").getImage();
    private final static Image CHANGE_IMAGE = new ImageIcon("data/hud_change.png").getImage();
    private final static Font FONT = Cache.getFont("Arial Unicode MS", Font.PLAIN, 25);
    
    private Quad defQuad;
    private Quad healthQuad;
    private Quad botCountQuad;
    private Quad itemTeleQuad;
    private Quad itemChangeQuad;
    private Quad timeCountQuad;
    
    private String definition;
    private String solution;
    
    /** Creates a new instance of Hud 
     * @param rootNode 
     */
    public DefaultHud(Node rootNode) {
        
        node = new Node("HUD");
        
        int width = DisplaySystem.getDisplaySystem().getWidth();
        int height = DisplaySystem.getDisplaySystem().getHeight();
        
        Rectangle defPos = new Rectangle(0, 65, 1024, 32);
        node.attachChild(defQuad = getQuad("def", defPos));
        
        Rectangle healthPos = new Rectangle(20, height - 20, 32, 128);
        node.attachChild(healthQuad = getQuad("health", healthPos));
        
        Rectangle timeCountPos = new Rectangle(70, height - 20, 200, 32);
        node.attachChild(timeCountQuad = getQuad("timeCount", timeCountPos));
        
        Rectangle botCountPos = new Rectangle(70, height - 60, 100, 32);
        node.attachChild(botCountQuad = getQuad("botCount", botCountPos));
        
        Rectangle itemTelePos = new Rectangle(width - 150, height - 20, 128, 32);
        node.attachChild(itemTeleQuad = getQuad("teleCount", itemTelePos));
        
        Rectangle itemChangePos = new Rectangle(width - 150, height - 60, 128, 32);
        node.attachChild(itemChangeQuad = getQuad("teleCount", itemChangePos));
        
        rootNode.attachChild(node);
        
        node.addController(new Controller() {
            private float time = 0;
            private int seconds = 0;
            public void update(float f) {
                float currentTime = time + f;
                int currentSec = (int) FastMath.floor(currentTime);
                if (seconds < currentSec) {
                    seconds = currentSec;
                    LevelManager.getStatistics().setSeconds(seconds);
                    showTimeCount(seconds);
                    if (seconds % 10 == 0) {
                        System.gc();
                    }
                }
                time = currentTime;
            }
        });
    }
    
   public void showDefinition(String definition) {
        this.definition = definition;
        this.solution = "";
        int dx = 20;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(1024, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        int width = bg.getFontMetrics().stringWidth(definition);
        bg.setColor(new Color(50,80,50,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.ORANGE);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        bg.drawString(definition, dx, dy);
        bg.dispose();
        
        setBufferedImage(bi, defQuad);
    }
    
    public void addSolution(String letter) {
        solution = solution + letter;
        int dx = 20;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(1024, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        int width = bg.getFontMetrics().stringWidth(definition + " - " + solution);
        bg.setColor(new Color(50,80,50,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        bg.setColor(Color.ORANGE);
        bg.drawString(definition + " - ", dx, dy);
        int start = bg.getFontMetrics().stringWidth(definition + " - " );
        bg.setColor(Color.CYAN);
        bg.drawString(solution, dx + start, dy);
        bg.dispose();
        
        setBufferedImage(bi, defQuad);
    }
    
    public void showBotCount(int num) {
        int dx = 10;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(100, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        String botStr = "# " + num;
        int width = bg.getFontMetrics().stringWidth(botStr);
        bg.setColor(new Color(50,50,80,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.ORANGE);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        bg.drawString(botStr, dx, dy);
        bg.dispose();
        
        setBufferedImage(bi, botCountQuad);
    }
    
    private void showTimeCount(int time) {
        int dx = 10;
        int dy = 24;
        
        BufferedImage bi = new BufferedImage(200, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setFont(FONT);
        String timeString = String.format("%02d:%02d",time / 60, time % 60);
        int width = bg.getFontMetrics().stringWidth(timeString);
        bg.setColor(new Color(50,50,80,130));
        bg.fillRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(dx - 10, 1, width + 22, 30, 5, 5);
        bg.setColor(Color.ORANGE);
        bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        bg.drawString(timeString, dx, dy);
        bg.dispose();
        
        setBufferedImage(bi, timeCountQuad);
    }
    
    
    public void showHealth(int health) {
        BufferedImage bi = new BufferedImage(32, 128, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setColor(new Color(255,0,0,130));
        bg.fillRoundRect(1, 101 - health, 30, health, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(1, 1, 30, 100, 5, 5);
        bg.dispose();
        
        setBufferedImage(bi, healthQuad);
    }
    
    public void showTeleporters(int teleporter) {
        showItem(TELEPORT_IMAGE, itemTeleQuad, teleporter);
    }
    
    public void showChange(int defChanges) {
        showItem(CHANGE_IMAGE, itemChangeQuad, defChanges);
    }
    
    private void showItem(Image image, Quad quad, int count) {
        BufferedImage bi = new BufferedImage(128, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D bg = (Graphics2D) bi.getGraphics();
        bg.setColor(new Color(80,80,80,130));
        bg.fillRoundRect(5, 1, 121, 30, 5, 5);
        bg.setColor(Color.BLACK);
        bg.setStroke(new BasicStroke(2));
        bg.drawRoundRect(5, 1, 121, 30, 5, 5);
        for (int i = 0; i < count; i++) {
            bg.drawImage(image, 90 - 40*i, 0, 32, 32, null);
        }
        bg.dispose();
        
        setBufferedImage(bi, quad);
    }
    
    
    
}
